local nl = require'luaNumeric'
require 'luaFDTD3D'
local plot = require "Q2DViewerEx"

----------------------------------------------------------------------------------------------------------------------------------------
--初始化参数
local SignalView={
	funs = {};
	xAxis = 0;
	ytype = 3;
	xtype = 1;
};
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function SignalView:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	o:init();
	return o;
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function SignalView:init ()
	self.funs = {};
	self.funs.abs = function(input) return nl.abs(input); end;
	self.funs.phs = function(input) return nl.arg(input)*180/pi; end;
	self.funs.real = function(input) return nl.real(input); end;
	self.funs.imag = function(input) return nl.imag(input); end;
	self.funs.log = function(input) 
		local  d = nl.abs(input);
		d = 20* nl.log10(d)
		return d; 
	end;
	self.fun_names = {};
	self.fun_names[1] = 'log';
	self.fun_names[2] = 'abs';
	self.fun_names[3] = 'phs';
	self.fun_names[4] = 'real';
	self.fun_names[5] = 'imag';	
	self.ytype = 4;
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--更新数据
function SignalView:refresh_figs ()
	--self.signal:update();
	--print( self.signal )
	self.results = self.signal:results();
	self.results.times = self.results.times /Geometry.ref_units_time()
	self:plot()
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--画图
function SignalView:plot ()
	self.view:clearGraphs();
	local x = self.results.times;
	--if self.xtype ==2 then
		--x = self.results.lambda;
	--end
	local fun = self.funs[ self.fun_names[self.ytype] ];
	--assert( self.results.data['Sout'], '数据不存在！' );
	self.view:addGraph(x,  fun( self.results.tfields ), 'time fields' );
	self.view:rescaleAxes();
	self:set_axis();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--画图
function SignalView:rescale ()
	self:update_parameters_from_ui();
	self:plot();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--坐标设置
function SignalView:set_axis()
	if self.xtype ==2 then
		self.view.xAxis:setProperty( {name="波长 ["..Geometry.ref_units_dim_str()..']'} )
	else
		self.view.xAxis:setProperty( {name="时间 ["..Geometry.ref_units_time_str()..']'} )
	end
	
	if self.ytype ==1 then
		self.view.yAxis:setProperty( {name="Excitation fields [dB]"} )
	elseif self.ytype ==2 then
		self.view.yAxis:setProperty( {name="Excitation fields [Mag.]"} )
	elseif self.ytype ==3 then
		self.view.yAxis:setProperty( {name="Argument of excitation fields [arc.]"} )
	elseif self.ytype ==4 then
		self.view.yAxis:setProperty( {name="Real part of excitation fields"} )
	elseif self.ytype ==5 then
		self.view.yAxis:setProperty( {name="Imag part of excitation fields"} )
	end
	 self.view:rescaleAxes();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--导出
function SignalView:export_data (path)
	print(path);
	self.signal:save(path);
end
----------------------------------------------------------------------------------------------------------------------------------------

return SignalView;
